<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dhmc" label="" class="">
          <a-input v-model:value="formState.dhmc" class="w-64" placeholder="单号/供应商名称" @keydown.enter="getData" />
        </a-form-item>
        <a-form-item name="dateRange" label="制单日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="dhmc" label="" class="ml-8">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="物品编号/物品名称/助记码" @keydown.enter="getData" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2" type="primary" @click="onAdd"> 新增 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 1800 }" column-code="000261" :pagination="pagination" @change="onChangeTable">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'NO'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'jhdh'">
            <PopoverTable :config="detailConfig" :params="{ jhdh: record.jhdh }" :width="1600">
              <template #bodyCell="{ column: popColumn, record: popRecord }">
                <template v-if="popColumn.dataIndex === 'mc'">
                  <MedicineInfo :value="{ mc: popRecord.mc, gg: popRecord.gg, cj: popRecord.cj }" />
                </template>
              </template>
              {{ record.jhdh }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'jhzt'">
            <a-tag v-if="record.jhzt === 0" color="green">待确认</a-tag>
            <a-tag v-if="record.jhzt === 1" color="blue">已确认</a-tag>
            <a-tag v-if="record.jhzt === 2" color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'djlb'">
            <a-tag v-if="record.djlb === '0'" color="green">药材</a-tag>
            <a-tag v-if="record.djlb === '1'" color="blue">物资</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" :disabled="record.jhzt !== 0" @click="onDetail(record)">明细</a-button>
              <BaseButton confirm-text="是否确认此计划单?" type="link" size="small" :disabled="record.jhzt !== 0" @click="onConfirm(record)"> 确认 </BaseButton>
              <BaseButton confirm-text="是否作废此计划单?" type="text" danger size="small" :disabled="record.jhzt !== 0" @click="onClickCancel(record)">
                作废
              </BaseButton>
              <TableMoreButton>
                <a-button type="link" size="small" :disabled="record.jhzt !== 0" @click="onEdit(record)">修改</a-button>
              </TableMoreButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <AddModal v-model:open="open" :row-data="rowData" @ok="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseButton from '@/components/BaseButton'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'
import AddModal from './AddModal.vue'

const router = useRouter()
const { empName } = useUserStore()
const rowData = ref(null)
const formRef = ref(null)
const open = ref(false)
const formState = ref({
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 10)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  pxzd: '',
  pxlx: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/materials/psiform/getSupplyPlanHeadList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      pxlx: params.pxlx, // 排序类型1正序2倒序
      pxzd: params.pxzd //排序字段 1 2 3
    }
  }
})
const detailConfig = ref({
  url: '/materials/psiform/getSupplyPlanDetailList',
  columnCode: '000141',
  rowKey: 'jhmxdh',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      NO: index + 1
    }))
  }
})
const onChangeTable = (pagination, filters, sorter) => {
  if (sorter.column?.dataIndex) {
    formState.value.pxlx = sorter.order === 'ascend' ? '1' : '2'
    if (sorter.column.dataIndex === 'jhdh') {
      formState.value.pxzd = '1'
    } else if (sorter.column.dataIndex === 'qrsj') {
      formState.value.pxzd = '2'
    } else {
      formState.value.pxzd = '3'
    }
  } else {
    formState.value.pxlx = ''
  }
  getData()
}

const onDetail = (record) => {
  router.push({
    path: '/material/warehouse/plan-detail',
    query: {
      jhdh: record.jhdh
    }
  })
}
const onClickCancel = async (record) => {
  const { code, msg } = await post('/materials/psiform/doCancelSupplyPlan', {
    jhdh: record.jhdh
  })
  if (code !== 0) return message.error(msg || '作废失败')
  getData()
  message.success('作废成功')
}
const onEdit = (record) => {
  rowData.value = Object.assign({}, record)
  open.value = true
}
const onAdd = () => {
  rowData.value = null
  open.value = true
}

const onConfirm = async (record) => {
  const { code, msg } = await post('/materials/psiform/doConfirmSupplyPlan', {
    jhdh: record.jhdh,
    qrr: empName
  })
  if (code !== 0) return message.error(msg || '确认失败')
  getData()
  message.success('确认成功')
}
</script>
